import axios, { type AxiosInstance } from 'axios'

// 写接口（增删改）
export const writeApi: AxiosInstance = axios.create({
  baseURL: 'http://localhost:5213/api',
  timeout: 15000,
})

// 读接口（查询）
export const readApi: AxiosInstance = axios.create({
  baseURL: 'http://localhost:5101/api',
  timeout: 15000,
})

// 可选：统一请求拦截/响应拦截（两者共用）
const attachInterceptors = (instance: AxiosInstance) => {
  instance.interceptors.request.use((config) => {
    // 在此注入鉴权等信息
    // const token = localStorage.getItem('token')
    // if (token) config.headers = { ...config.headers, Authorization: `Bearer ${token}` }
    return config
  })

  instance.interceptors.response.use(
    (resp) => resp,
    (error) => {
      // 这里可以统一弹错或上报
      return Promise.reject(error)
    }
  )
}

attachInterceptors(writeApi)
attachInterceptors(readApi)

// 导出一个简单的注册模板 API 集合（可按需扩展）
export const registrationSlipApi = {
  create: (data: any) => writeApi.post('/registration-slip-templates', data),
  update: (data: any) => writeApi.put('/registration-slip-templates', data),
  remove: (templateId: number, deletedBy: string) =>
    writeApi.delete('/registration-slip-templates', { params: { templateId, deletedBy } }),
  // 示例查询：
  getById: (id: number) => readApi.get(`/registration-slip-templates/${id}`),
  list: () => readApi.get('/registration-slip-templates'),
}


